package com.xiangty.helloworld.hello;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.FileSystemResource;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.io.File;

/**
 * @Description:
 * @Author: xiangty
 * @Date: 2018-09-25
 */
@Service
public class MailService {

    private static final Logger LOGGER = LoggerFactory.getLogger(MailService.class);

    /**
     * 发送人的邮箱
     */
    @Value("${spring.mail.username}")
    private String from;

    @Autowired
    private JavaMailSender mailSender;

    public void sayHello() {
        System.out.println("Hello World...");
    }

    /**
     * 发送文本邮件
     *
     * @param to      收件人邮箱
     * @param subject 标题
     * @param content 内容
     */
    public void sendSimpleMail(String to, String subject, String content) {
        LOGGER.info("发送文本邮件开始，参数：{}, {}, {}", to, subject, content);

        SimpleMailMessage message = new SimpleMailMessage();
        message.setTo(to);
        message.setSubject(subject);
        message.setText(content);
        message.setFrom(from);
        mailSender.send(message);

        LOGGER.info("发送文本邮件成功");
    }

    /**
     * 发送html邮件
     *
     * @param to
     * @param subject
     * @param content
     */
    public void sendHtmlMail(String to, String subject, String content) {
        LOGGER.info("发送html邮件开始，参数：{}, {}, {}", to, subject, content);
        try {
            MimeMessage message = mailSender.createMimeMessage();

            MimeMessageHelper helper = new MimeMessageHelper(message, true);
            helper.setTo(to);
            helper.setSubject(subject);
            helper.setText(content, true);
            helper.setFrom(from);
            mailSender.send(message);
            LOGGER.info("发送html邮件成功");
        } catch (MessagingException e) {
            LOGGER.error("发送静态邮件失败，{}", e);
        }
    }

    /**
     * 发送附件邮件
     *
     * @param to
     * @param subject
     * @param content
     * @param filePath 文件地址
     */
    public void sendAttachmentsMail(String to, String subject, String content, String filePath) {
        LOGGER.info("发送附件邮件开始，参数：{}, {}, {}, {}", to, subject, content, filePath);
        try {
            MimeMessage message = mailSender.createMimeMessage();

            MimeMessageHelper helper = new MimeMessageHelper(message, true);
            helper.setTo(to);
            helper.setSubject(subject);
            helper.setText(content, true);
            helper.setFrom(from);

            FileSystemResource file = new FileSystemResource(new File(filePath));
            if (file != null) {
                String fileName = file.getFilename();
                // 如果 要带多个附件，那么就可以在此处配置多个addAttachment
                helper.addAttachment(fileName, file);
            }
            mailSender.send(message);
            LOGGER.info("发送附件邮件成功");
        } catch (MessagingException e) {
            LOGGER.error("发送附件邮件失败，{}", e);
        }
    }


    /**
     * 发送图片邮件
     *
     * @param to
     * @param subject
     * @param content
     * @param rscPath
     * @param rscId
     * @throws MessagingException
     */
    public void sendInlinResourceMail(String to, String subject, String content, String rscPath, String rscId) {
        LOGGER.info("发送图片邮件开始，参数：{}, {}, {}, {}, {}", to, subject, content, rscPath, rscId);
        try {
            MimeMessage message = mailSender.createMimeMessage();

            MimeMessageHelper helper = new MimeMessageHelper(message, true);
            helper.setFrom(from);
            helper.setTo(to);
            helper.setSubject(subject);
            helper.setText(content, true);

            FileSystemResource res = new FileSystemResource(new File(rscPath));
            if (res != null) {
                //如果需要发送多个图片，将此处多写几次，需要将考虑修改rscId和rscPath的传参
                helper.addInline(rscId, res);
            }
            mailSender.send(message);
            LOGGER.info("发送图片邮件成功");
        } catch (MessagingException e) {
            LOGGER.error("发送图片邮件失败，{}", e);
        }
    }

}
